#include "BankSystem.hh"
#include <fstream>
#include <iomanip>

bool BankSystem::registerCustomer(const std::string& name) {
    if (customers.find(name) != customers.end()) {
        return false;
    }

    auto savingsAcc = std::make_shared<SavingsAccount>(name);
    auto checkingAcc = std::make_shared<CheckingAccount>(name);
    
    customers[name] = std::make_pair(savingsAcc, checkingAcc);
    accountsById[savingsAcc->getAccountId()] = savingsAcc;
    accountsById[checkingAcc->getAccountId()] = checkingAcc;
    
    return true;
}

bool BankSystem::accountExists(const std::string& accountId) const {
    return accountsById.find(accountId) != accountsById.end();
}

std::shared_ptr<IAccount> BankSystem::getAccount(const std::string& accountId) {
    auto it = accountsById.find(accountId);
    return (it != accountsById.end()) ? it->second : nullptr;
}

std::pair<std::shared_ptr<SavingsAccount>, std::shared_ptr<CheckingAccount>>
BankSystem::getCustomerAccounts(const std::string& name) {
    auto it = customers.find(name);
    if (it != customers.end()) {
        return it->second;
    }
    return std::make_pair(nullptr, nullptr);
}

void BankSystem::generateTransactionReport(const std::string& filename) const {
    std::ofstream file(filename);
    if (!file) return;

    file << "===== Global Transaction Report =====" << std::endl;
    for (const auto& [id, account] : accountsById) {
        for (const auto& trans : account->getTransactions()) {
            file << "Transaction ID: " << trans.getTransactionId() << std::endl
                 << "Type: " << trans.getType() << std::endl
                 << "Amount: ¥" << std::fixed << std::setprecision(2) << trans.getAmount() << std::endl
                 << "From: " << trans.getSourceAccountId() << std::endl
                 << "To: " << trans.getDestinationAccountId() << std::endl
                 << "Note: " << trans.getNote() << std::endl
                 << "------------------------" << std::endl;
        }
    }
}